package com.base.cn.platform.os.controller.classes.topic.reply;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.classes.Classes;
import com.base.cn.platform.os.entity.classes.condition.ClassesMemberCondition;
import com.base.cn.platform.os.entity.classes.member.ClassesMember;
import com.base.cn.platform.os.entity.classes.topic.ClassesTopic;
import com.base.cn.platform.os.entity.classes.topic.reply.ClassesTopicReply;
import com.base.cn.platform.os.entity.course.cou.buy.CourseBuyRecord;
import com.base.cn.platform.os.service.classes.ClassesBiz;
import com.base.cn.platform.os.service.classes.member.ClassesMemberBiz;
import com.base.cn.platform.os.service.classes.topic.ClassesTopicBiz;
import com.base.cn.platform.os.service.classes.topic.reply.ClassesTopicReplyBiz;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 班级话题回复
 *
 * @author SK
 * @since 2018-05-21
 */
@RestController
@RequestMapping("/course")
public class ClassesTopicReplyController extends BaseController {

    @Autowired
    private ClassesBiz classesBiz;
    @Autowired
    private ClassesMemberBiz classesMemberBiz;
    @Autowired
    private ClassesTopicBiz classesTopicBiz;
    @Autowired
    private ClassesTopicReplyBiz classesTopicReplyBiz;
    @Autowired
    private CourseBuyRecordBiz courseBuyRecordBiz;

    /**
     * 查询话题评论列表
     *
     * @param classesTopicReply 班级话题评论查询条件
     * @param currentPage       第N页
     * @param pageSize          每页数据量
     * @param setCreator        设置评论人
     * @param setLeaf           设置评论的回复
     * @param setAllLeaf        设置评论的所有回复
     * @return 话题评论列表
     */
    @RequestMapping("/findClassesTopicReplyListPage")
    public PageInfo<ClassesTopicReply> findClassesTopicReplyListPage(@RequestBody ClassesTopicReply classesTopicReply,
                                                                     @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                                     @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                                     @RequestParam("setCreator") boolean setCreator,
                                                                     @RequestParam("setLeaf") boolean setLeaf,
                                                                     @RequestParam("setAllLeaft") boolean setAllLeaf,
                                                                     @RequestParam("isFront") boolean isFront) {
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        if (DataUtil.isPositive(pageSize)) {
            pagination.setPageSize(pageSize);
        }
        return classesTopicReplyBiz.findClassesTopicReplyPage(classesTopicReply, pagination, setCreator, setLeaf, setAllLeaf,isFront);
    }

    /**
     * 保存话题评论
     *
     * @param topicReply 话题评论
     * @return 保存结果
     */
    @RequestMapping("/saveClassesTopicReply")
    public Map<String, Object> saveClassesTopicReply(@RequestBody ClassesTopicReply topicReply,
                                                     @RequestParam("setCreator") boolean setCreator) {
        Map<String, Object> check = this.checkClassesTopicReply(topicReply);
        if (check != null) {
            return check;
        }
        return classesTopicReplyBiz.saveClassesTopicReply(topicReply, setCreator);
    }

    /**
     * 更新话题评论状态
     *
     * @param ids    话题评论id
     * @param status 1.正常 2.删除
     * @return 操作结果
     */
    @RequestMapping("/updateClassesTopicReplyStatus")
    public Map<String, Object> updateClassesTopicReplyStatus(@RequestParam("ids") String ids,
                                                             @RequestParam("status") Integer status) {
        ids = DataUtil.filterNotNumberStr(ids);
        if (StringUtils.isEmpty(ids)) {
            return ResultUtil.ERROR("选择操作的话题评论");
        }
        classesTopicReplyBiz.updateClassesTopicReplyStatus(ids, status);
        return ResultUtil.SUCCESS("删除话题评论成功");
    }

    /**
     * 检查班级话题评论数据
     *
     * @param classesTopicReply 班级话题评论
     */
    private Map<String, Object> checkClassesTopicReply(ClassesTopicReply classesTopicReply) {
        if(StringUtils.isEmpty(classesTopicReply.getContent())){
            return ResultUtil.ERROR("评论内容不能为空");
        }
        if (!(DataUtil.idIsNotNull(classesTopicReply.getTopicId()) &&
                DataUtil.idIsNotNull(classesTopicReply.getUserId()))) {
            return ResultUtil.ERROR("参数错误");
        }
        ClassesTopic topic = classesTopicBiz.findById(classesTopicReply.getTopicId());
        if (topic.getStatus() != 1 || topic.getReply() != 1) {
            return ResultUtil.ERROR("话题暂不能评论");
        }

        // 班级状态判断
        Classes classes = classesBiz.findById(topic.getClassesId());
        if (classes.getStatus() != 1) {
            return ResultUtil.ERROR("班级已停班，不可以评论了");
        }

        //自由回复(被封禁者不可回复)
        if(topic.getReplyPermission() == 1){
            boolean isBanned = classesMemberBiz.isBanned(topic.getClassesId(),classesTopicReply.getUserId());
            if(!isBanned){
                return ResultUtil.ERROR("你被禁止在本班评论");
            }
        }

        // 权限判断(仅本班成员回复)
        if (topic.getReplyPermission() == 2) {
            ClassesMemberCondition condition = new ClassesMemberCondition();
            condition.identities(1, 2);
            condition.setClassesId(topic.getClassesId());
            condition.setMemberId(classesTopicReply.getUserId());
            List<ClassesMember> memberList = classesMemberBiz.findClassesMemberList(condition, false);
            if (ObjectUtils.isEmpty(memberList)) {
                return ResultUtil.ERROR("仅限本班成员才能评论");
            }
            if (memberList.get(0).getStatus() != 1) {
                return ResultUtil.ERROR("你被禁止在本班评论");
            }
            // 课程购买有效期判断
            if(!classes.isFree()){
                BigDecimal courseId = classes.getCourseId();
                CourseBuyRecord courseBuyRecord = courseBuyRecordBiz.checkUserBuyCourse(classesTopicReply.getUserId(), courseId);
                if (Objects.isNull(courseBuyRecord)) {
                    return ResultUtil.ERROR("你购买的课程已到期，不可以评论了");
                }
            }
        }
        return null;
    }
}
